// packageStore/pages/distribution/sub/index.js
import BasePage from '../../../js/common/base-page';
import {api} from '../../../js/util';
import { distribution} from "../../../js/modules/distribution.js";
const app = getApp();
BasePage({

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    tablist: ['一级客户（0）', '二级客户（0）'],
    activeIndex: 0,
    status:1,//1:一级客户，2：二级客户
    pageIndex:1,
    pageSize:15,
    hasMoreData: true,
    sliderOffset: 0,
    sliderLeft: 10,
    slidewidth: 0,
    list:[],
    Currency:'',//货币符号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    wx.getSystemInfo({
      success: (res)=> {
        that.setData({
          slidewidth: (res.windowWidth / that.data.tablist.length) - 20,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadList()
    this.setData({
      Currency:api.Currency || '￥'
    })
  },
loadList() {
  var pararm = {
    status: this.data.status,
    pageIndex:this.data.pageIndex,
    pageSize:this.data.pageSize
  }
  var that=this
  distribution.loadSubOrdinate(pararm, function (content) {
    var {list,tablist} = that.data;
    tablist[0] = "一级客户（"+content.layonecount+'）';
    tablist[1] = "二级客户（"+content.laytwocount+'）';
    that.setData({
      tablist:tablist
    });
    if (that.data.pageIndex == 1) {
      list = [];
    }
    if (content.data.length > 0) {
      var contentlist = content.data;
      if (contentlist.length < that.data.pageSize) {
        that.setData({
          list: list.concat(contentlist),
          hasMoreData: false,
          layonecount: content.layonecount,
          laytwocount: content.laytwocount,
        });
      } else {
        that.setData({
          list: list.concat(contentlist),
          hasMoreData: true,
          pageIndex: that.data.pageIndex + 1,
          layonecount: content.layonecount,
          laytwocount: content.laytwocount,
        });
      }

    } else {
      that.setData({
        list: [],
        hasMoreData: true,
        pageIndex: 1
      });
    }
  });
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.pageIndex = 1;
    this.loadList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.hasMoreData) { 
      this.loadList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 切换当前选择项
   */
  changeCurrent: function (res) {
    if (this.data.activeIndex == res.detail.currentNum.id) return;
    var activeIndex = res.detail.currentNum.id;
    var status=activeIndex==0?1:2;
    var that=this
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: activeIndex,
      status:status,
      pageIndex:1
    },()=>{
      that.loadList()
    });
  }
})